package bacnetnetwork1;

import com.serotonin.bacnet4j.LocalDevice;
import com.serotonin.bacnet4j.exception.BACnetServiceException;
import com.serotonin.bacnet4j.obj.BACnetObject;
import com.serotonin.bacnet4j.type.Encodable;
import com.serotonin.bacnet4j.type.enumerated.EngineeringUnits;
import com.serotonin.bacnet4j.type.enumerated.ObjectType;
import com.serotonin.bacnet4j.type.enumerated.PropertyIdentifier;
import com.serotonin.bacnet4j.type.primitive.CharacterString;
import com.serotonin.bacnet4j.type.primitive.Real;


public class Controller extends LocalDevice{
    
    
    
    public Controller(int ID, String networkAddress, String deviceAddress, int port, String name, EngineeringUnits units, int outputsNumber, int refInputsNumber, int controlParametersNumber) throws Exception {
        super(ID, networkAddress,deviceAddress);
        this.getConfiguration().setProperty(PropertyIdentifier.objectName,
                new CharacterString(name));
        this.setPort(port);
        
        
        for (int i=1;i<outputsNumber+1;i++) {
            BACnetObject ao = new BACnetObject(this, this.getNextInstanceObjectIdentifier(ObjectType.analogOutput));
            ao.setProperty(PropertyIdentifier.objectName, new CharacterString("Controller output "+i));
            ao.setProperty(PropertyIdentifier.units, units);
            this.addObject(ao);
        }
        
        for (int i=1;i<refInputsNumber+1;i++) {
            BACnetObject av = new BACnetObject(this,
                    this.getNextInstanceObjectIdentifier(ObjectType.analogValue));
            av.setProperty(PropertyIdentifier.objectName, new CharacterString("Reference value "+i));
            av.setProperty(PropertyIdentifier.relinquishDefault, new Real(0.0000F));
            this.addObject(av);
        }
        
        for (int i=1;i<controlParametersNumber+1;i++) {      
            BACnetObject av = new BACnetObject(this,
                this.getNextInstanceObjectIdentifier(ObjectType.analogValue));
            av.setProperty(PropertyIdentifier.objectName, new CharacterString("Control parameter "+i+" value"));
            av.setProperty(PropertyIdentifier.relinquishDefault, new Real(0.0000F));
            this.addObject(av);
        }

        
        // Start device
        this.initialize();        
    }
    
    public Encodable getReferenceValue(int n) throws BACnetServiceException {        
        BACnetObject av=this.getObject("Reference value "+n); 
        Encodable currentValue=av.getProperty(PropertyIdentifier.presentValue);
        return currentValue;        
    }
    
    public Encodable getControlParameter1Value(int n) throws BACnetServiceException {        
        BACnetObject av=this.getObject("Control parameter "+n+" value"); 
        Encodable currentValue=av.getProperty(PropertyIdentifier.presentValue);
        return currentValue;        
    }    
    
    
    public void setControllerOutput(int n, double givenValue) throws BACnetServiceException {
        BACnetObject ai=this.getObject("Controller output "+n); 
        ai.setProperty(PropertyIdentifier.presentValue, new Real((float) givenValue));
    }
    
}

